import scrapy
from mzt_demo.items import MztDemoItem
from mzt_demo.pipelines.DownloadMztPipeline import DownloadMztPipeline
import re
import os


class MztSpider(scrapy.Spider):
    name = r"mzt"

    # allowed_domains = [r'http://www.mzitu.com/156041']
    # start_urls = [r'http://www.mzitu.com/158530/18']
    image_urls = []

    def __init__(self, start=None, *args, **kwargs):
        super(MztSpider, self).__init__(*args, **kwargs)
        for i in start.split(','):
            t0 = i.split('/')
            self.start_urls.append('/'.join(t0[0:4]))
        # ...

    def parse(self, response):
        # print(response)
        main_img = response.xpath('//div[@class="main-image"]/p/a/img')[0]
        item = MztDemoItem()
        item['name'] = main_img.xpath('@alt')[0].extract()
        current_image_url = main_img.xpath('@src')[0].extract()
        item['img_url'] = current_image_url

        nav = response.xpath('//div[@class="pagenavi"]/a')
        last = nav[len(nav) - 2]
        base = last.xpath('@href')[0].extract()
        item['base'] = re.sub("\/\d+$", "", base)

        t0 = response.url.split('/')
        if len(t0) > 4:
            current = int(t0[4])
        else:
            current = 1

        total = int(last.xpath('span/text()').extract()[0])
        item['total'] = total
        item['current'] = os.path.basename(item['img_url'])
        # print(item)
        pipe_item = DownloadMztPipeline()
        pipe_item['image_urls'] = [current_image_url]
        yield pipe_item
        # print ("cur %d , total %d" % (current, total))
        if current < total:
            next = current + 1
            yield scrapy.Request("%s/%d" % ('/'.join(t0[0:4]), next), callback=self.parse)
